package com.aminography.redirectglide

import com.bumptech.glide.load.Options
import com.bumptech.glide.load.model.GlideUrl
import com.bumptech.glide.load.model.ModelLoader
import com.bumptech.glide.load.model.ModelLoader.LoadData
import com.bumptech.glide.load.model.ModelLoaderFactory
import com.bumptech.glide.load.model.MultiModelLoaderFactory
import okhttp3.Call
import okhttp3.OkHttpClient
import java.io.InputStream

/**
 * A simple model loader for fetching media over http/https using OkHttp.
 */
class OkHttpUrlLoader(
    private val client: Call.Factory
) : ModelLoader<GlideUrl, InputStream> {

    override fun handles(url: GlideUrl): Boolean {
        return true
    }

    override fun buildLoadData(model: GlideUrl, width: Int, height: Int, options: Options): LoadData<InputStream>? {
        return LoadData(model, OkHttpStreamFetcher(client, model))
    }

    /**
     * The default factory for [OkHttpUrlLoader]s.
     *
     * Constructor for a new Factory that runs requests using a static singleton client.
     *
     * @param client this is typically an instance of `OkHttpClient`.
     */
    class Factory @JvmOverloads constructor(
        private val client: Call.Factory = internalClient
    ) : ModelLoaderFactory<GlideUrl, InputStream> {

        override fun build(multiFactory: MultiModelLoaderFactory): ModelLoader<GlideUrl, InputStream> {
            return OkHttpUrlLoader(client)
        }

        override fun teardown() {
            // Do nothing, this instance doesn't own the client.
        }

        companion object {
            private val internalClient: Call.Factory = OkHttpClient().newBuilder().followRedirects(false).followSslRedirects(false).build()//modify by muziling
        }
    }

}